﻿namespace com.stifel.ServerBuildMgmt.BuildInfo
{
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using Microsoft.SharePoint;

    using com.stifel.Extensions.StringExtensions;
    using com.stifel.ServerBuildMgmtDao;

    public partial class BuildInfoUserControl : UserControl
    {
        #region Methods

        protected void onItemCommand( object sender, RepeaterCommandEventArgs e )
        {
            if ( e.CommandName == "Edit" )
            {
                Summary s = new Summary( );
                ServerValues server = e.CommandArgument.ToString( ).FromBase64String( ).DeserializeFromXml<ServerValues>( );
                uiCurrentSever.Value = e.CommandArgument.ToString( );
                Label1.Text = s.getSummaryWithOutOriginalValues( server );
                uiCblBuildInfo_Load( );
                if ( server.Build.CompletedFlag.HasValue )
                {
                    uiCbBuildCompletedFlag.Checked = server.Build.CompletedFlag.Value;
                }
                else
                {
                    uiCbBuildCompletedFlag.Checked = false;
                }
                uiBuild.Visible = false;
                uiBuildUpdatePanel.Visible = true;
                setDefaultButton( );
            }
        }

        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !Page.IsPostBack )
            {
                isChanged.Value = false.ToString( );
                if ( HttpContext.Current.Request.QueryString[ "ItemGuid" ] == null )
                {
                    PageLoadBackUP( );
                }
                else if ( HttpContext.Current.Request.QueryString[ "ItemGuid" ] == string.Empty )
                {
                    PageLoadBackUP( );
                }
                else
                {
                    try
                    {
                        Guid ItemGuid = HttpContext.Current.Request.QueryString[ "ItemGuid" ].ToGuid( );
                        StifelServerBuildDao dao = new StifelServerBuildDao( );
                        StifelVirtualServerRequest request = dao.getRequestFromListUsingGUID( ItemGuid );
                        if ( request != null )
                        {
                            uiLitValRequest.Text = request.Id.Value.ToString();
                            Summary s = new Summary( );
                            VirtualServerCollectionClass v = request.getRequestInfoDeserialized( );
                            List<RepeaterBinding> summaryBinder = s.getSummaryWithOutOriginalValues( v );
                            if ( request.getBuildStatus( ) == true )
                            {
                                myRepeater.DataSource = summaryBinder;
                                myRepeater.DataBind( );
                                uiBuild.Visible = true;
                                uiBuildUpdatePanel.Visible = false;
                                uiBackBadUrlRequest.Visible = false;
                                setSubmitValidation( );
                            }
                            else
                            {
                                myRepeater.DataSource = summaryBinder;
                                myRepeater.DataBind( );
                                uiBuild.Visible = true;
                                uiBuildUpdatePanel.Visible = false;
                                uiBackBadUrlRequest.Visible = false;
                                foreach ( RepeaterItem r in myRepeater.Items )
                                {
                                    ImageButton ie = ( ImageButton ) r.FindControl( "editButton" );
                                    ie.ImageUrl = "~/_layouts/com.stifel.ServerBuildMgmt/images/editg20x20grey.png";
                                    ie.Enabled = false;
                                }
                                uiBtnSave.Enabled = false;
                                uiBtnSubmit.Enabled = false;
                                uiLitNotification.Text = "Cannot Make Any Changes.";
                            }
                        }
                    }
                    catch ( Exception ex )
                    {
                        PageLoadBackUP( );
                        string errorMsg = ex.Message;
                    }
                }
                isSomethingChanged( );
                setUrlReferrer( );
            }
        }

        protected void uiBtnCancel_Click( object sender, EventArgs e )
        {
            Page.Response.Redirect( UrlReferrer );
        }

        protected void uiBtnSave_Click( object sender, EventArgs e )
        {
            SaveServers( );
        }

        protected void uiBtnSubmit_Click( object sender, EventArgs e )
        {
            SubmitServers( );
        }

        protected void uiCancelButton_Click( object sender, ImageClickEventArgs e )
        {
            uiBuild.Visible = true;
            uiBuildUpdatePanel.Visible = false;
            uiBackBadUrlRequest.Visible = false;
            uiLblBuildErrorMsg.Text = string.Empty;
        }

        protected void uiNavigationTab_Click( object sender, EventArgs e )
        {
            Button button = sender as Button;
            if ( button != null )
            {
                string arg = button.CommandArgument;
                navigateToSelectedView( button );
                //summarize( arg );
            }
        }

        protected void uiOkButton_Click( object sender, ImageClickEventArgs e )
        {
            bool ok = ValidateBuild( );
            if ( !ok )
            {
                uiCbBuildCompletedFlag.Checked = false;
                uiLblBuildErrorMsg.Text = "Build Incomplete. Please Verify";
            }
            else
            {
                uiLblBuildErrorMsg.Text = string.Empty;
                ServerValues server = uiCurrentSever.Value.FromBase64String( ).DeserializeFromXml<ServerValues>( );
                VirtualServerCollectionClass v = new VirtualServerCollectionClass( );
                foreach ( RepeaterItem r in myRepeater.Items )
                {
                    HiddenField hiddenField = ( HiddenField ) r.FindControl( "valuePart" );
                    ServerValues currentCompare = hiddenField.Value.FromBase64String( ).DeserializeFromXml<ServerValues>( );
                    if ( currentCompare.ServerBasics.ServerName.Equals( server.ServerBasics.ServerName ) )
                    {
                        //currentCompare.Build.BuildInfo.Clear( );
                        foreach ( RepeaterItem ri in uiBuildCheckListRepeater.Items )
                        {
                            HiddenField uiHiddenFieldSelected = ( HiddenField ) ri.FindControl( "uiHiddenFieldSelected" );
                            Label uiLblBuildCheckList = ( Label ) ri.FindControl( "uiLblBuildCheckList" );
                            CheckBox uiCbBuildCheckList = ( CheckBox ) ri.FindControl( "uiCbBuildCheckList" );
                            if ( uiHiddenFieldSelected.Value.Equals( true.ToString( ), StringComparison.InvariantCultureIgnoreCase ) )
                            {

                            }
                            else
                            {
                                if ( uiCbBuildCheckList.Checked )
                                {
                                    BuildUserInfo bui = new BuildUserInfo( );
                                    bui.CompletedTask = uiLblBuildCheckList.Text.Trim( );
                                    bui.CurrentLoggedInUser = SPContext.Current.Web.CurrentUser.LoginName.ToString( );
                                    bui.CurrentDateTime = DateTime.Now.ToString( );
                                    currentCompare.Build.BuildInfo.Add( bui );
                                }
                            }
                        }
                        //foreach ( ListItem li in uiCblBuildInfo.Items )
                        //{
                        //    if ( li.Selected )
                        //    {
                        //        currentCompare.Build.BuildInfo.Add( li.Text.Trim( ) );
                        //    }
                        //}
                        if ( uiCbBuildCompletedFlag.Checked )
                        {
                            currentCompare.Build.CompletedFlag = true;
                        }
                        else
                        {
                            currentCompare.Build.CompletedFlag = false;
                        }
                    }
                    v.virtualServerCollection.Add( currentCompare );
                }
                Summary s = new Summary( );
                List<RepeaterBinding> summaryBinder = s.getSummaryWithOutOriginalValues( v );
                myRepeater.DataSource = summaryBinder;
                myRepeater.DataBind( );
                uiBuild.Visible = true;
                uiBuildUpdatePanel.Visible = false;
                uiBackBadUrlRequest.Visible = false;
                isChanged.Value = true.ToString( );
                setSubmitValidation( );
                isSomethingChanged( );
                uiLblBuildErrorMsg.Text = string.Empty;
            }
        }

        protected void uiBuildCheckList_ItemDataBound( object sender, RepeaterItemEventArgs e )
        {
            if ( ( e.Item.ItemType == ListItemType.Item ) || ( e.Item.ItemType == ListItemType.AlternatingItem ) )
            {
                Label uiLblBuildCheckList = ( Label ) e.Item.FindControl( "uiLblBuildCheckList" );
                Label uiLblSpaces = ( Label ) e.Item.FindControl( "uiLblSpaces" );
                CheckBox uiCbBuildCheckList = ( CheckBox ) e.Item.FindControl( "uiCbBuildCheckList" );
                HiddenField uiHiddenFieldSelected = ( HiddenField ) e.Item.FindControl( "uiHiddenFieldSelected" );
                ServerValues CurrentServerValues = uiCurrentSever.Value.FromBase64String( ).DeserializeFromXml<ServerValues>( );
                uiLblSpaces.Visible = false;
                uiLblBuildCheckList.Visible = false;
                uiCbBuildCheckList.Visible = true;
                uiHiddenFieldSelected.Value = false.ToString( );
                if ( ( CurrentServerValues.Build.BuildInfo != null ) && ( CurrentServerValues.Build.BuildInfo.Count != 0 ) )
                {
                    foreach ( BuildUserInfo bui in CurrentServerValues.Build.BuildInfo )
                    {
                        if ( bui.CompletedTask.Equals( uiLblBuildCheckList.Text.Trim( ), StringComparison.InvariantCultureIgnoreCase ) )
                        {
                            uiLblSpaces.Visible = true;
                            uiLblBuildCheckList.Visible = true;
                            uiCbBuildCheckList.Visible = false;
                            uiHiddenFieldSelected.Value = true.ToString( );
                        }
                    }
                }
            }
        }

        #endregion Methods

        #region Other

        //protected void uiCblBuildInfo_SelectedIndexChanged( object sender, EventArgs e )
        //{
        //    bool ok = ValidateBuild( );
        //    if ( !ok )
        //    {
        //        uiCbBuildCompletedFlag.Checked = false;
        //        uiLblBuildErrorMsg.Text = string.Empty;
        //    }
        //    else
        //    {
        //        uiLblBuildErrorMsg.Text = string.Empty;
        //    }
        //    //summarize(uiBtnBuildInfo.CommandArgument);
        //}
        //protected void uiCbBuildCompletedFlag_CheckedChanged( object sender, EventArgs e )
        //{
        //    bool ok = ValidateBuild();
        //    if ( !ok )
        //    {
        //        uiCbBuildCompletedFlag.Checked = false;
        //        uiLblBuildErrorMsg.Text = "Build Incomplete. Please Verify";
        //    }
        //    else
        //    {
        //        uiLblBuildErrorMsg.Text = string.Empty;
        //    }
        //    //summarize( uiBtnBuildInfo.CommandArgument );
        //}

        #endregion Other
    }
}